﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Text;
using DAL.Entity;
using DAL.Persistencia;
using System.Globalization;
using System.Drawing;



namespace BLL.Utils
{
    public class FotoResize
    {

        public void RetornaImgHTL(string endFoto, string endTemp)
        {


            System.Drawing.Image img = System.Drawing.Image.FromFile(@endFoto);

            Size tam = new Size(323, 255);

            ResizeImage(img, tam, endFoto, endTemp, false);
        }


        public static System.Drawing.Image ResizeImage(System.Drawing.Image image, Size size, string caminhoimagem, string caminhosalvar, bool preserveAspectRatio = true)
        {
            int newWidth;
            int newHeight;
            if (preserveAspectRatio)
            {
                int originalWidth = image.Width;
                int originalHeight = image.Height;
                float percentWidth = (float)size.Width / (float)originalWidth;
                float percentHeight = (float)size.Height / (float)originalHeight;
                float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
                newWidth = (int)(originalWidth * percent);
                newHeight = (int)(originalHeight * percent);
            }
            else
            {
                newWidth = size.Width;
                newHeight = size.Height;
            }
            System.Drawing.Image newImage = new Bitmap(newWidth, newHeight);
            using (Graphics graphicsHandle = Graphics.FromImage(newImage))
            {
                graphicsHandle.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
            }

            string temp;

            temp = caminhosalvar + ".tmp";
            newImage.Save(temp, System.Drawing.Imaging.ImageFormat.Jpeg);
            image.Dispose();
            //File.Delete(caminhoimagem);
            //File.Copy(temp, caminhoimagem);
            //File.Delete(temp);

            return newImage;
        }

    }
}
